package com.axend.aerosense.home.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.impl.IOperationResult;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.base.viewmodel.BaseViewModelFactroy;
import com.axend.aerosense.common.base.ITitle;
import com.axend.aerosense.common.bean.AlarmBean;
import com.axend.aerosense.common.bean.LogType;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.home.viewmodel.HomeAlarmDetailsFragmentViewModel;
import com.axend.aerosense.home.R;
import com.axend.aerosense.home.databinding.HomeFragmentAlarmDetailsBinding;

public class HomeAlarmDetailsFragment extends MvvmBaseFragment<HomeFragmentAlarmDetailsBinding, HomeAlarmDetailsFragmentViewModel>
        implements ILoadDataResult<AlarmBean> {
    public final static String ALARM = "ALARM";
    private AlarmBean alarmBean;

    public static HomeAlarmDetailsFragment getInstance(AlarmBean alarmBean){
        HomeAlarmDetailsFragment fragment = new HomeAlarmDetailsFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ALARM, alarmBean);
        fragment.setArguments(bundle);   //设置参数
        return fragment;
    }

    @Override
    protected void initParameters() {
        super.initParameters();
        Bundle bundle = getArguments();
        if(null != bundle){
            this.alarmBean = (AlarmBean)bundle.getSerializable(ALARM);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.home_fragment_alarm_details;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }


    @Override
    protected HomeAlarmDetailsFragmentViewModel getViewModel() {
        BaseViewModelFactroy<AlarmBean> myViewModelFactroy = new BaseViewModelFactroy<>(this, this);
        HomeAlarmDetailsFragmentViewModel tmp = new ViewModelProvider(this, myViewModelFactroy).get(HomeAlarmDetailsFragmentViewModel.class);
        tmp.setLogUuid(alarmBean.getLogUuid());
        return tmp;
    }

    @Override
    protected void onRetryBtnClick() {
        showLoading();
        viewModel.onLoad();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    private void init() {
        viewDataBinding.homeButAlarmDel.setOnClickListener(v -> {
            viewModel.onDeleteLog(getActivity(), new IOperationResult() {
                @Override
                public void onSuccess() {
                    LiveDatabus.getInstance().with(Gobal.EVENT_KEY.WARN_EVENT_CHANGE, Integer.class).postValue(1);
                    LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_HOME, Integer.class).postValue(1);
                    LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_ROOM, Integer.class).postValue(1);
                    getActivity().finish();
                }

                @Override
                public void onFail() {

                }
            });
        });

        setLoadSir(viewDataBinding.homeContentView);
        showLoading();
        viewModel.onLoad();
    }

    @Override
    public void loadDataSuccess(AlarmBean alarmBean) {
        if(null == alarmBean){
            return;
        }
        //设置数据
        if (alarmBean != null) {
            String title = alarmBean.getEventTypeName();// + getString(R.string.common_alarm);
            ((ITitle)getActivity()).setTitle(title);
        }

//        viewDataBinding.homeTvPromptInfo.setText(alarmBean.getAlertTips());
        viewDataBinding.setData(alarmBean);
        viewDataBinding.executePendingBindings();
        if(alarmBean.getAlertLogState() == Gobal.STATUS_CODE.UNREAD){
            LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_HOME, Integer.class).postValue(1);
            LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REFESH_ROOM, Integer.class).postValue(1);
        }

    }

    @Override
    public void loadDataFail(String decs) {

    }
}
